from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
from typing import Optional
import uvicorn
from  CalcCore import CalcCore

# 创建 FastAPI 实例
app = FastAPI(
    title="示例API",
    description="一个简单的FastAPI演示",
    version="1.0.0"
)

# 内存数据库（模拟）
fake_db = {
    1: {"id": 1, "name": "苹果", "price": 5.5, "category": "水果"},
    2: {"id": 2, "name": "胡萝卜", "price": 2.3, "category": "蔬菜"}
}

# 定义数据模型
class Item(BaseModel):
    name: str
    price: float
    category: Optional[str] = None

class UpdateItem(BaseModel):
    name: Optional[str] = None
    price: Optional[float] = None
    category: Optional[str] = None

# 根路由
@app.get("/")
async def root():
    return {
        "message": "欢迎使用商品API,Hello !!!",
        "endpoints": [
            "GET /items",
            "GET /items/{id}",
            "POST /items",
            "PUT /items/{id}",
            "DELETE /items/{id}"
        ]
    }

# 获取所有商品
@app.get("/items")
async def get_items(category: Optional[str] = None):
    if category:
        return [item for item in fake_db.values() if item["category"] == category]
    return list(fake_db.values())

# 获取单个商品
@app.get("/items/{item_id}")
async def get_item(item_id: int):
    if item_id not in fake_db:
        raise HTTPException(status_code=404, detail="商品不存在")
    return fake_db[item_id]

# 创建新商品
@app.post("/items", status_code=201)
async def create_item(item: Item):
    new_id = max(fake_db.keys()) + 1
    fake_db[new_id] = {"id": new_id, **item.dict()}
    return {"message": "商品创建成功", "data": fake_db[new_id]}

# 更新商品
@app.put("/items/{item_id}")
async def update_item(item_id: int, item: UpdateItem):
    if item_id not in fake_db:
        raise HTTPException(status_code=404, detail="商品不存在")
    
    update_data = item.dict(exclude_unset=True)
    fake_db[item_id].update(update_data)
    
    return {"message": "商品更新成功", "data": fake_db[item_id]}

# 删除商品
@app.delete("/items/{item_id}")
async def delete_item(item_id: int):
    if item_id not in fake_db:
        raise HTTPException(status_code=404, detail="商品不存在")
    
    deleted_item = fake_db.pop(item_id)
    return {"message": "商品已删除", "deleted_item": deleted_item}

# 简单的测试demo
@app.get("/mingzhuinfo/{date_str}")
async def mingzhuinfo(date_str: str):
     
     #TOD: date_Str 处理
     #。。。。。。
     # ---简单的示例----
     # frame= CalcCore.PaipanLayout(date_str)
     # return frame;

     #  date_str --->  mingzao
     # |—_
     # var gongWei = ZiWeiPaiPan.ZiWeiPan.GetTianPan_Layout(mingzao); //紫微排盘
     # return gongWei

     yinshou= CalcCore.GetYanShouByTianGan("甲")
     return {
        "name":'zhangsan',
        "sex":'乾造',
        "nongli":'九二年三月初一辰时',
        "date":f'{date_str}',
        "yinshou":f'{yinshou}',
        "tanpan":{

            "子":{
                "ganzhi":"甲子",
                "startList":[]
            }
            #....
        }
     }



if __name__ == "__main__":
    uvicorn.run(
        "main:app",  # 改为字符串格式 "文件名:应用实例名"
        host="0.0.0.0",
        port=8000,
        reload=True,
        access_log=True
    )